limsLocationBoard.choosePage = {

    getGridParam: function () {
        var _this = this, columns = limsLocationBoard.getChooseListColumns();
        _this.param.single ? columns.unshift({
            radio: true
        }) : columns.unshift({
            checkbox: true
        });
        var requestData = _this.param
        requestData.createdByOrgId_EQ = workspace.user.ext$.loginorgid
        return {
            type: 'grid',
            id: 'lims-location-board-choose-list-grid',
            url: limsLocationBoard.baseUrl + '/queries/choosable',
            requestData: requestData,
            page: true,
            columns: columns,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getComp('sample-Location-choose').setData([]);
                }
            },
            onRowActive: function (index, row) {
                Gikam.postSync(limsLocationBoard.holeUrl + '/queries', Gikam.getJsonWrapper({
                    f: {
                        boardId_EQ: row.id
                    },
                    n: -1, s: -1
                })).done(function (data) {
                    if (Gikam.isEmpty(data.rows)) {
                        Gikam.getComp('sample-Location-choose').rowNum = 0;
                    } else {
                        Gikam.getComp('sample-Location-choose').setData(data.rows);
                    }
                });
            },
            onRendered: function () {
                if (_this.param.single) {
                    this.hideToolbar();
                }
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.CONFIRM',
                icon: 'select',
                onClick: function () {
                    let selections = Gikam.getComp('lims-location-board-choose-list-grid').getSelections();
                    let size = selections.reduce((sum, e) => sum + Number(e.freeHoleCount || 0), 0);
                    if (size < _this.param.size) {
                        Gikam.alert('GIKAM.LOCATION.ASSIGN.NO_MANY_HOLE');
                        return
                    }
                    Gikam.getLastModal().close(selections);
                }
            }, {
                type: 'form',
                id: 'sample-form',
                fields: [{
                    field: 'boardCode_EQ',
                    title: 'T_LIMS_LOCATION_BOARD.BOARDCODE'
                }]
            }]
        }
    },

    getHoleTab: function () {
        var _this = this;
        return {
            type: 'sampleLocation',
            id: 'sample-Location-choose',
            bgColor: {
                in: 'green',
                out: 'blue',
            },
            rowNum: 0,
            colNum: 0,
            data: [],
            onDblClick: function (data) {
                if (_this.param.single) {
                    if (data.stockType && data.stockType != 'free') {
                        Gikam.alert('MODULE.BUSSISE.SAMPLE.BUTTON.ASSIGN.CHOOSE_UNASSIGNED');
                        return;
                    }
                    data.positionType = 'board';
                    Gikam.getLastModal().close(data);
                }
            },
        }
    },

    create: function () {
        var _this = this;
        if (_this.param.type === 'board') {
            Gikam.create('layout', {
                id: 'lims-location-board-choose-list-layout',
                renderTo: Gikam.getLastModal().window.$dom,
                west: {
                    width: '40%',
                    items: [this.getGridParam()]
                },
                center: {
                    title: 'MODULE.BASEDATA.LOCATION.TREE.LOCATIONS.HOLE',
                    items: [_this.getHoleTab()]
                }
            });
            return;
        }
        Gikam.create('layout', {
            id: 'lims-location-board-choose-list-layout',
            renderTo: Gikam.getLastModal().window.$dom,
            center: {
                title: 'LIMS.TAB.LOCATION.BOARD',
                items: [{
                    type: 'layout',
                    west: {
                        width: '50%',
                        items: [this.getGridParam()]
                    },
                    center: {
                        title: 'MODULE.BASEDATA.LOCATION.TREE.LOCATIONS.HOLE',
                        items: [_this.getHoleTab()]
                    }
                }]
            }
        });
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
};
